
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:SetPlayer(ply)

	if not ply then self:Remove() end

	self:SetPhysicsAttacker(ply)
	self:SetOwner(ply)
	
end


/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()

	self:SetModel( "models/gibs/manhack_gib03.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS ) 
	//self:SetCollisionGroup( COLLISION_GROUP_WEAPON )
	self:SetName("medusa")
	
	self.ATimer = CurTime() + math.Rand(.2,.4)
	self.DoSound = false
	self.Iterate = .8
	self.Closest = nil
	self.ClosestLen = 0
	self.Face = false
	
	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
	end
	
	local rfire = "weapons/rpg/rocketfire1.wav"
	//local rel = "vehicles/tank_readyfire1.wav"  reload sound

	self:EmitSound(Sound(rfire),100,math.random(170,215))
	
	local trail = ents.Create( "env_spritetrail" )
	
	trail:SetPos( self:GetPos() + (self:GetForward() * -5) )
	trail:SetParent( self )

	trail:SetKeyValue( "targetname", "" )
	trail:SetKeyValue( "parentname", "" )
	trail:SetKeyValue( "lifetime" , "0.38" )
	trail:SetKeyValue( "startwidth", "5.0" )
	trail:SetKeyValue( "endwidth" , "2.5" )
	trail:SetKeyValue( "spritename", "sprites/bluelaser1.vmt" )
	trail:SetKeyValue( "renderamt", "205" )
	trail:SetKeyValue( "rendermode", "5" )
	trail:SetKeyValue( "rendercolor", "189 189 120" )
	trail:SetName("medusa")
	trail:Spawn()
	self.Trail = trail
	
	self.Closest = self:LockTarget()
	
	if self.Closest then
		self.ATimer = nil
	end
end

function ENT:DoExplode()

	local expl = {"ambient/levels/prison/inside_battle8.wav",
	"weapons/explode3.wav",
	"weapons/explode4.wav",
	"weapons/explode5.wav"}
	
	local rexpl = expl[math.random(1,4)]
	util.PrecacheSound(rexpl)
	
	self:ExplosionDamage(self:GetOwner(),self:GetPos(),10 + math.random(1,8),100,6000,rexpl,math.random(125,175))
	
	local effectdata = EffectData()
	effectdata:SetOrigin(self:GetPos())
	util.Effect("medusa_explosion", effectdata)
	
	//self:Remove()  This causes spam
	self.Trail:Fire("kill",1,0.001)
	self:Fire("kill",1,0.0001)
	self.StopThink = true

end

function ENT:SetTarget( ent )
	if ent:IsPlayer() then return end
	self.Closest = ent
	self.ATimer = nil
end

function ENT:GetTarget()
	if self.Closest != nil then return end
	self.Closest = nil
	self.ClosestLen = 90000
	self.CLen = 0
	
	local pos = self:GetPos()
	local ang = self:GetAngles()
	local tracedata = {}
	tracedata.start = pos + Vector(0,0,-90)
	tracedata.endpos = pos + Vector(0,0,-90000)
	local trace = util.TraceLine(tracedata)
	local distance = pos - trace.HitPos
	local dlength = math.Round((distance:Length()/1.4) + 500)
	
	local locator = ents.FindInSphere( pos, dlength )
	local targets = {}
	for k,v in pairs(locator) do
		if (string.find(v:GetClass(),"vehicle") or string.find(v:GetClass(),"beacon")) then
			table.insert(targets,v)
		end
	end
	
	for k,v in pairs(targets) do
		if (v:IsValid()) and v.Owner then
			self.Closest = v
			self.ClosestLen = 0
			return
		end
	end
end

function ENT:PhysicsSimulate(phys, frametime)

	phys:Wake()
	phys:EnableGravity(false)
	
	if self:WaterLevel() > 0 then
		self.StopThink = true
	end
	
	local f = 500
	if self.Iterate > 20 then f = 800 end
	
	local forward = self:GetForward()*f*self.Iterate
	self.Iterate = self.Iterate + .1
	
	if self.Closest and self.Closest:IsValid() then
		if self.Face then
			if math.random(1,5) != 5 then
				self:PointAtEntity(self.Closest)
			end
		end
	end
	
	phys:ApplyForceCenter(forward)
	
	if (self.ATimer != nil) and (self.ATimer < CurTime()) then
		self:GetTarget()
		self.ATimer = nil
    elseif self.ATimer == nil then
		if self.Closest == nil then
			self.ATimer = CurTime() + (math.random(1,3)/10)
			util.PrecacheSound("buttons/button17.wav")
			self:EmitSound("buttons/button17.wav",80,math.random(100,110))
		return end
		
		if (!self.Closest:IsValid()) then //invalid entity
			self.StopThink = true
		return end
		
		self.Face = true
		
		local phys = self:GetPhysicsObject()
		phys:ApplyForceCenter(forward)
		
		if phys:IsValid() then
			phys:ApplyForceCenter(self:GetForward() * 2000)
		end
	end
end


function ENT:Think()

	if self.StopThink then self:DoExplode() return end
 
end 

function ENT:PhysicsCollide( data, physobj )

	self.StopThink = true
	
end